//
//  MBCircleBTNCell.m
//  发布求租
//
//  Created by MB__Lee on 2018/6/6.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBCircleBTNCell.h"

#import "MBCircleBTN.h"

@interface MBCircleBTNCell()
@property (weak, nonatomic) IBOutlet UILabel *MBTLabel;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hightConstant;

@property (weak, nonatomic) IBOutlet UILabel *redLabel;



//三个按钮
@property(nonatomic,strong)UIButton *oneBTN;
@property(nonatomic,strong)UIButton *twoBTN;
@property(nonatomic,strong)UIButton *threeBTN;

@end


@implementation MBCircleBTNCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.hightConstant.constant = 43;
}
-(void)setBTNModel:(MBCircleBTNModel *)BTNModel{
    [super setBTNModel:BTNModel];
    
    // 1. 是否有 *
    if (BTNModel.hasX) {
        self.redLabel.hidden = NO;
    }else{
        self.redLabel.hidden = YES;
    }
    
    
//    for (UIView *btn in self.subviews) {
//        if ([btn isKindOfClass:[MBCircleBTN class]]) {
//            [btn removeFromSuperview];
//        }
//    }
    
    //2. 根据 数组内容生成按钮
    if (BTNModel.btnNameMut.count) {
        
        CGFloat y = self.hightConstant.constant;
        
        CGFloat margin = 15;
        CGFloat widht = 80;
        CGFloat hight = 32;
        
        UIButton *lastBTN = [[UIButton  alloc] initWithFrame:CGRectMake(0, y, 0, 0)];
        
        for (int i = 0; i< BTNModel.btnNameMut.count; i++) {
            
            CGFloat x = CGRectGetMaxX(lastBTN.frame);
            CGFloat y = lastBTN.frame.origin.y;
            x += margin;
            
            
            MBCircleBTN *circlebtn  =[MBCircleBTN buttonWithType:UIButtonTypeCustom];
            circlebtn.frame = CGRectMake(x, y, widht, hight);
            circlebtn.layer.cornerRadius = 0.5 * hight;
            circlebtn.clipsToBounds = YES;
            [circlebtn setTitle:BTNModel.btnNameMut[i] forState:UIControlStateNormal];
            circlebtn.tag = 100 + i;
            
            [circlebtn addTarget:self action:@selector(addTitleToSource:) forControlEvents:UIControlEventTouchUpInside];
            
            [self addSubview:circlebtn];
            
            if (i == 0 ) {
                _oneBTN = circlebtn;
            }else if (i == 1){
                _twoBTN = circlebtn;
            }else if (i == 2){
                _threeBTN = circlebtn;
            }
            
            
            
            lastBTN = circlebtn;
        }
    }
    
    [self makeStaticSelectedWithIndex:self.selectedIndex];
    
}

-(void)addTitleToSource:(UIButton *)btn{
    
    if ([self.delegate respondsToSelector:@selector(MBCircleBTNCellDelegateSendButtonIndex:)]) {
        [self.delegate MBCircleBTNCellDelegateSendButtonIndex:btn.tag-100];
    }
    
}


/**

  进行默认设置 -- 让此 cell 默认选择 某个按钮
 @param index 下标
 */
-(void)makeStaticSelectedWithIndex:(NSInteger)index{
    
    if (index == 0) {
        _oneBTN.selected = YES;
        _twoBTN.selected = NO;
        _threeBTN.selected = NO;
    }else if (index == 1){
        _oneBTN.selected = NO;
        _twoBTN.selected = YES;
        _threeBTN.selected = NO;
    }else if (index == 2){
        _oneBTN.selected = NO;
        _twoBTN.selected = NO;
        _threeBTN.selected = YES;
    }
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
